package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.ICategoryService;
import com.itheima.reggie.service.IDishService;
import com.itheima.reggie.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    private IDishService dishService;
    private ISetmealService setmealService;

    @PostMapping
    public R save(@RequestBody Category category, HttpSession httpSession) {
        log.info("执行添加");
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        Employee userLogin = (Employee) httpSession.getAttribute("userLogin");
//        category.setCreateUser(userLogin.getId());
//        category.setUpdateUser(userLogin.getId());

        boolean save = categoryService.save(category);
        if (save) {
            return R.success(category);
        }
        return R.error("添加失败");
    }


    @GetMapping("/page")
    public R pageQuery(Integer page, Integer pageSize) {
        //添加页构造器
        IPage<Category> pageInfo = new Page<>(page, pageSize);
        log.info("正在分页查询");
        categoryService.page(pageInfo);
        return R.success(pageInfo);

    }

    /**
     * .
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R delete(Long id) {
        log.info("收到删除请求" + id);
        int delete = categoryService.delete(id);
        if (delete == 0) {
            return R.error("当前分类下关联了菜品，不能删除");
        } else if (delete == 1) {
            return R.error("当前分类下关联了套餐，不能删除");
        }
        return R.success("删除成功");
    }

    /**
     * 根据ID修改分类信息
     *
     * @param category
     * @param session
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category, HttpSession session) {
        log.info(category.toString());
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        Employee userLogin = (Employee) session.getAttribute("userLogin");
//        category.setCreateUser(userLogin.getId());
//        category.setUpdateUser(userLogin.getId());
        boolean b = categoryService.updateById(category);
        if (b) {

            return R.success("修改成功");
        }
        return R.error("修改失败");
    }


    /**
     * 根据条件查询分类数据
     *
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R selectList(Category category) {
        log.info("selectList" + category.toString());
        LambdaQueryWrapper<Category> lqw = Wrappers.lambdaQuery(Category.class)
                .eq(category.getType() != null, Category::getType, category.getType());
        List<Category> categories = categoryService.list(lqw);
        return R.success(categories);
    }


}
